<?php
/**
 * Klasse zur Realisierung von Partials fuer einen MVC-Konstrukt
 *
 * @TODO, @NOTICE: Veraltete Klasse, bitte nicht mehr verwenden !!!
 *
 * @author		Joachim Ruf
 * @copyright	Copyright (c) 2003-2013 Loresoft Software (http://www.loresoft.de)
 * @license		http://yapary.loresoft.de/lizenzbedingungen     New BSD License
 *
 * */


Class Yapary_MVC_Partial Implements Yapary_MVC_IPartial
{
    private $oView       = NULL;
    private $oModel      = NULL;
    private $pathPartial = NULL;
    /** @object $oMVCConfig		: MVC-Konfigurationsklasse */
    private $oMVCConfig  = NULL;
    private $arrException = array();


    function __construct (Yapary_MVC_View &$_cView, Yapary_MVC_Model &$_cModel, $_pathPartial, Yapary_MVC_MVCConfig &$_cMVCConfig)
    {
	    Yapary_TraceRoute::add();

	    $this->oView = $_cView;
        $this->oModel = $_cModel;
        $this->pathPartial = $_pathPartial;
		$this->oMVCConfig= $_cMVCConfig;
    }


	/**
	 * Gibt das View-Objekt zurueck
	 * @return Yapary_MVC_View
	 * */
    private function view ()
    {
	    Yapary_TraceRoute::add();

	    return $this->oView;
    }


	/**
	* Laedt das angegebene Partial
	* @param string $pathPartial : Pfad zum Partial inklusive Dateiendung
	* @param array $arrVariable : Mitzuliefernde Werte array('varName1'=>val1, 'varName2'=>val2, ...)
	* @return string
	*/
    public function insert ($fileName, array $arrVars = array())
    {
	    Yapary_TraceRoute::add();

	    $result = '';

        if ($this->exists($fileName)) {
			if (count($arrVars) >= 1) {
				while (list($key, $val) = each($arrVars)) {
					$$key = $val;
				}
			}

            ob_start();
    			if ($this->oMVCConfig->enableIdentification) {
        			echo '<div class="componentCheckPartial" title="Partial: '.$this->oMVCConfig->partialPath.$fileName.'">';
        			include($this->pathPartial . $fileName);
        			echo '</div>';
    			} else {
        			include($this->pathPartial . $fileName);
    			}
            $result = ob_get_contents();
            ob_end_clean();
		} else {
			throw new Exception('<pre>Das Partial ' . $this->pathPartial . $fileName . ' existiert nicht</pre>');
		}

		return $result;
    }


    /**
     * Prueft ob das angegebene Partial existiert
     * @param string $fileName : das im Partial-Ordner befindliche Partial
     * @return bool
     * */
    public function exists ($fileName)
    {
        if (is_file($this->pathPartial . $fileName)) {
            return true;
        }

        return false;
    }


	/**
	 * Leitet den Zugriff auf alle vorhandenen Methoden weiter
	 * Wichtig: nur bei Methoden ohne Parameterweitergabe moeglich
	 * {model()|partial()|variable|exchange()}
	 * @return Object|NULL
	 * */
	public function __get ($objName)
	{
	    $arrValid = array('view', 'exchange');

	    if (method_exists($this, $objName) && in_array($objName, $arrValid)) {
	        return $this->$objName();
	    } elseif ($objName == 'partial') { // Damit Zugriff in Partial auf $this->partial->insert(...) lauten kann
	        return $this;
	    } else {
	        $this->arrException[] = new Exception('Methode ' . $objName . '() existiert nicht.');
	    }
	}


	/**
	 * Gibt das Exchange-Objekt zurueck
	 * @return Yapary_MVC_Exchange
	 * */
	protected function exchange ()
	{
		if ($this->oView->exchange === NULL) {
			$this->arrException[] = new Exception('Exchangeobjekt existiert nicht');
			return NULL;
		}

		return $this->oView->exchange;
	}


	/**
	 * Gibt das Model-Objekt zurueck
	 * @return Yapary_MVC_Model
	 * */
	protected function model ()
	{
	    Yapary_TraceRoute::add();

	    if ($this->oModel === NULL) {
			$this->arrException[] = new Exception('Model existiert nicht');
			return NULL;
		}

		return $this->oModel;
	}


	/**
	* Liefert alle ausgeloesten Exceptions
	* @return Array[Exception]
	*/
	public function &getExceptionList ()
	{
		return $this->arrException;
	}


	/**
	* Liefert die Anzahl der vorhandenen Exceptions
	* @return int
	*/
	public function getExceptionCount ()
	{
		return count($this->arrException);
	}


}

?>